//
//  SEGMenuListCellManager.m
//  SEGUhomeStandard
//
//  Created by 乔杨 on 2019/5/28.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGMenuListCellManager.h"
#import "SEGMenuListCellDelegate.h"
#import "SEGMenuOneRowsTableViewCell.h"
#import "SEGMenuTwoRowsTableViewCell.h"
#import "SEGMenuThreeRowsTableViewCell.h"
#import "SEGMenuFourRowsNomalTableViewCell.h"
#import "SEGMenuFiveRowsTableViewCell.h"
#import "SEGMenuFourRowsScrollTableViewCell.h"
#import "SEGMenuThreeRowsScrollTableViewCell.h"
#import "SEGColorBlockView.h"
#import "SEGMenuListServiceTitleTableViewCell.h"
#import "SEGPropertyInformTableViewCell.h"
#import "SEGSharedNeihborhoodModel.h"
#import "SEGAdvertisementView.h"
#import "SEGMenuSideslipView.h"

#import "SEGHLBCommunityPlaceholderCell.h"

#import "SEGCommunityShoppingMallTableViewCell.h"
#import "SEGMenuListRecommendBannerTableViewCell.h"

#import "SEGPropertyNoticeVerticalRollView.h"//滚动物业通知
#import "SEGMenuListMineIntegralTableViewCell.h"//积分商城
#import "SEGHeaderView.h"
#import "SEGHLBMarketBriefTableViewCell.h"
#import "SEGHLBActivityListTableViewCell.h"

// cell估算高度
CGFloat const kMenuListCell_estimatedHeight = 88.f;
// header默认高度
CGFloat const kMenuListHeaderView_Height = 5.f;
// footer默认高度
CGFloat const kMenuListFooterView_Height = CGFLOAT_MIN;
// cell类型字典
static NSDictionary *_cellDict = nil;
@implementation SEGMenuListCellManager

+ (NSDictionary *)menuList_getCellDict {
    if (!_cellDict) {
        _cellDict = @{
                      @(SEGEnumShowTypeAD):[SEGAdvertisementViewCell class],  // 广告
                      @(SEGEnumShowTypeNormal):[SEGMenuOneRowsTableViewCell class], // 一行一列
                      @(SEGEnumShowTypeCommunityShoppingMall):[SEGCommunityShoppingMallTableViewCell class], // 4.8.0 社区商城
                      @(SEGEnumShowTypeTwoColumsSpread):[SEGMenuTwoRowsTableViewCell class], // 一行两列 平铺
                      @(SEGEnumShowTypeFourColums):[SEGMenuFourRowsScrollTableViewCell class], // 一行四列 侧滑
                      @(SEGEnumShowTypeFourColumsSpread):[SEGMenuFourRowsNomalTableViewCell class],  // 一行四列 平铺
                      @(SEGEnumShowTypeThreeColums):[SEGMenuThreeRowsScrollTableViewCell class], // 一行三列 侧滑  -本地使用
                      @(SEGEnumShowTypeThreeColumsSpread):[SEGMenuThreeRowsTableViewCell class], // 一行三列 平铺
                      @(SEGEnumShowTypeHouseAuthentication):[SEGMenuOneRowsTableViewCell class], // 一行一列 小区认证跳转
                      @(SEGEnumShowTypePropertyNotification):[SEGPropertyInformTableViewCell class], // 4.7.0 物业消息展示
                      @(SEGEnumShowTypeFiveColumsSpread):[SEGMenuFiveRowsTableViewCell class],  // 一行五列 平铺
                      @(SEGEnumShowTypeServerHeader):[SEGMenuListServiceTitleTableViewCell class], // 服务业组头标题
                      @(SEGEnumShowTypePropertyScrollNotification):[SEGPropertyNoticeVerticalRollViewCell class],//滚动的物业通知
                      @(SEGEnumShowTypeColorBlock_HLB):[SEGColorBlockViewCell class], // 首页色块菜单控件
                      @(SEGEnumShowTypePropertyScrollNotification):[SEGPropertyNoticeVerticalRollViewCell class],//滚动的物业通知
                      @(SEGEnumShowTypeRecommendBanner_HLB):[SEGMenuListRecommendBannerTableViewCell class],
                      @(SEGEnumShowTypeHeader):[SEGHeaderViewCell class], // 我的tab页面头部
                      @(SEGEnumShowTypeMineIntegral_HLB):[SEGMenuListMineIntegralTableViewCell class],
                      @(SEGEnumShowTypeFleaMarketing):[SEGHLBMarketBriefTableViewCell class],
                      @(SEGEnumShowTypeCommunityActivity):[SEGHLBActivityListTableViewCell class],
                      @(SEGEnumShowTypeNavigationBar_99001):[UITableViewCell class], //导航栏
                      @(SEGEnumShowTypeNavigationBar_99002):[UITableViewCell class], //导航栏
                      @(SEGEnumShowTypeNavigationBar_99003):[UITableViewCell class], //导航栏
                      @(SEGEnumShowTypeNavigationBar_99004):[UITableViewCell class] //导航栏
                    };
    }
    return _cellDict;
}
+ (UITableViewCell *)menuList_tableView:(UITableView *)tableView
                  cellForRowAtIndexPath:(NSIndexPath *)indexPath
                       sectionDataCount:(NSUInteger)dataCount
                              menuModel:(SEGMenuListModel *)menuModel
                  currentViewController:(UIViewController *)viewController
                                   info:(id)info {
    _cellDict = [self menuList_getCellDict];
    NSNumber *widgetTypeKey = @(menuModel.widgetType.integerValue);
    if (!_cellDict[widgetTypeKey]) { // 无控件类型, 展示默认cell
        static NSString *cellId = @"cellId";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text =[NSString stringWithFormat:@"%ld_%ld_%@_%@",(long)indexPath.section, (long)indexPath.row, widgetTypeKey, menuModel.serviceName];
        return cell;
    } else if ([widgetTypeKey isEqual:@(SEGEnumShowTypeFleaMarketing)]) { // 跳蚤市场
        if ([menuModel.fleaMarketData isKindOfClass:[NSArray class]] && menuModel.fleaMarketData.count > 0) {
            SEGHLBMarketBriefTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSEGHLBMarketBriefCellIdentifier];
            if (!cell) {
                cell = [[SEGHLBMarketBriefTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSEGHLBMarketBriefCellIdentifier];
            }
            [cell updateUIWith:menuModel.fleaMarketData];
            cell.checkMarketDetailBlock = ^(SEGQueryGoodsIdelModel * _Nonnull fleaModel) {
                NSMutableDictionary *inputParams = [NSMutableDictionary dictionary];
                inputParams[@"objId"] = fleaModel.goodsId;
                inputParams[@"objType"] = [SEGBasicBussinessEnums getSEGEnumIdelExchange:(fleaModel.type.integerValue)];
                inputParams[@"operationType"] = @(SEGNeighbourhoodOperationType_Market);
                inputParams[@"showCommentView"] = @(NO);
                inputParams[@"delegate"] = viewController;
                inputParams[@"indexPath"] = indexPath;
                UIViewController *marketDetailVC = [[SEGUhomeMediator sharedInstance] seg_initNeihborhoodDetailViewController:inputParams];
                [viewController.navigationController pushViewController:marketDetailVC animated:YES];
            };
            return cell;
        } else {
            // 集市缺省图
            return [SEGHLBCommunityPlaceholderCell seg_initWithTableView:tableView style:SEGHLBCommunityStyle_Market];
        }
    } else if ([widgetTypeKey isEqual:@(SEGEnumShowTypeCommunityActivity)]) { // 活动
        if (menuModel.activityModel && [menuModel.activityModel isKindOfClass:[SEGActivityListModel class]]) {
            SEGHLBActivityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSEGHLBActivityListCellIdentifier];
            if (!cell) {
                cell = [[SEGHLBActivityListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSEGHLBActivityListCellIdentifier];
            }
            [cell setUpCellWithModel:menuModel.activityModel];
            return cell;
        } else {
            // 活动缺省图
            return [SEGHLBCommunityPlaceholderCell seg_initWithTableView:tableView style:SEGHLBCommunityStyle_Activity];
        }
    } else if ([widgetTypeKey isEqual:@(SEGEnumShowTypeAD)]) { // 广告
        SEGAdvertisementViewCell *cell = [SEGAdvertisementViewCell initWithTableView:tableView];
        SEGAdvertisementViewLayout *layout = [[SEGAdvertisementViewLayout alloc] initWithData:menuModel.advertisementData type:menuModel.advertisementData.adType];
        menuModel.advertisementData.totalHeight = layout.totalHeight;
        [cell setWithLayout:layout];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    } else if ([widgetTypeKey isEqual:@(SEGEnumShowTypePropertyScrollNotification)]){ // 滚动的物业通知
        SEGPropertyNoticeVerticalRollViewCell *cell = [SEGPropertyNoticeVerticalRollViewCell initWithTableView:tableView];
        SEGVerticalRollViewModel *ret = [[SEGVerticalRollViewModel alloc]init];
        ret.widgetHeight = 47 ;
        ret.businessData = menuModel.propertyNoticeData;
        
        SEGVerticalRollViewLayout *layout  = [[SEGVerticalRollViewLayout alloc] initWithData:ret type:SEGEnumVerticalRollViewType_NO];
        [cell setWithLayout:layout];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    UITableViewCell *cell;
    if ([_cellDict[widgetTypeKey] respondsToSelector:@selector(menuCell_initTableViewCellWithTableView:)]) {
        cell = [_cellDict[widgetTypeKey] menuCell_initTableViewCellWithTableView:tableView];
    } else {
        cell = [UITableViewCell new];
    }
    //只会刷新一次
    if ([cell respondsToSelector:@selector(menuCell_refreshCellWithIndexPath:sectionDataCount:menuModel:)]) {
        [(id<SEGMenuListCellDelegate>)cell menuCell_refreshCellWithIndexPath:indexPath sectionDataCount:dataCount menuModel:menuModel];
    }
    
    return cell;
}

@end
